package com.flushbonading.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flushbonading.pojo.User;
import com.flushbonading.mapper.UserMapper;
import com.flushbonading.pojo.dto.UserDTO;
import com.flushbonading.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlx
 * @since 2021-04-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    /**
     * 登录时进行验证
     * @param userDTO
     * @return
     */
    public User Login(UserDTO userDTO) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userDTO.getId());
        //md5加密验证
        wrapper.eq("password", SecureUtil.md5(userDTO.getPassword()));

        User user = userMapper.selectOne(wrapper);
        System.out.println(user);
        return user;
    }

    @Override
    public User getUserId(String userId) {
        return userMapper.getUserId(userId);
    }

    /**
     * 更新用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        user.setPassword(SecureUtil.md5(user.getPassword()));
        userMapper.updateUser(user);
    }

    /**
     * 同user_id查询用户权限
     * @param user_id
     * @return
     */
    @Override
    public User getRoleByUserId(String user_id) {

        return userMapper.getRoleByUserId(user_id);
    }

    @Override
    public int selectCountByRoleID(int role_id) {
        return userMapper.selectCountByRoleID(role_id);
    }

    @Override
    public int deleteAUser(String student_number) {
        return userMapper.deleteAUser(student_number);
    }

}
